export const useAutoGpApiCodeGeneration = () => {
    const generateAutoGpApiCode = (formData, tables) => {
      let code = '';
      
      console.log('接收到的表格数据:', JSON.stringify(tables, null, 2));
      
      // 添加程序头部注释
      code += `# 程序名称: ${formData.programId}\n`;
      code += `# 接口函数: ${formData.interfaceFunction}\n`;
      code += `# 撰写时间: ${new Date().toLocaleString()}\n\n`;
  
      code += `# 数据返回\n`;
      code += `DEFINE g_resp RECORD\n`;
      code += `    docno STRING\n`;
      code += `END RECORD\n\n`;
  
      code += `# 单号\n`;
      code += `DEFINE g_docno LIKE type_file.chr100\n`;
      code += `DEFINE p_inTransaction LIKE type_file.num5\n\n`;
  
      code += `DATABASE ds\n\n`;
      
      // 添加全局变量引用
      code += `GLOBALS "../../../tiptop/config/top.global"\n`;
      code += `GLOBALS "../../../tiptop/aws/4gl/aws_ttsrv2_global.4gl"\n\n`;
      
      // 添加 SQL 变量定义
      code += `DEFINE g_forupd_sql    STRING\n\n`;
  
      // 1. 先生成所有子表的类型定义
      const detailTables = tables.filter(table => table.name !== '主表');
      console.log('过滤出的子表:', JSON.stringify(detailTables, null, 2));
  
      detailTables.forEach(table => {
        code += `TYPE ${table.tableName} RECORD\n`;
        
        if (Array.isArray(table.data) && table.data.length > 0) {
          table.data.forEach((field, index, array) => {
            if (field && field.name) {
              const refField = `${field.referenceTableField}`;
              const isLastField = index === array.length - 1;
              code += `  ${field.name.padEnd(20)} LIKE ${refField}${isLastField ? '' : ', '}  #${field.description || field.name}\n`;
            }
          });
        }
        code += `END RECORD\n\n`;
      });
  
      // 2. 生成子表的数组定义
      detailTables.forEach(table => {
        if (table.arrayName && table.tableName) {
          code += `DEFINE ${table.arrayName} ${table.tableName}\n`;
        }
      });
      code += '\n';
  
      // 3. 生成主表的类型定义
      const mainTable = tables.find(table => table.name === '主表');
      console.log('找到的主表:', JSON.stringify(mainTable, null, 2));
      
      if (mainTable) {
        code += `TYPE ${mainTable.tableName} RECORD\n`;
        
        if (Array.isArray(mainTable.data) && mainTable.data.length > 0) {
          mainTable.data.forEach((field, index, array) => {
            if (field && field.name) {
              const refField = `${field.referenceTableField}`;
              const isLastField = index === array.length - 1 && detailTables.length === 0; // 如果没有子表，这就是最后一个字段
              code += `  ${field.name.padEnd(20)} LIKE ${refField}${isLastField ? '' : ', '} #${field.description || field.name}\n`;
            }
          });
        }
        
        // 添加子表数组
        detailTables.forEach((childTable, index) => {
          if (childTable.arrayName && childTable.tableName) {
            const isLast = index === detailTables.length - 1;
            code += `  ${childTable.arrayName.padEnd(20)} DYNAMIC ARRAY OF ${childTable.tableName}${isLast ? '' : ','}\n`;
          }
        });
        code += `END RECORD\n\n`;
      }
  
      // 4. 定义主表
      if (mainTable && mainTable.tableName) {
        code += `DEFINE g_master ${mainTable.tableName}\n\n`;
      }
  
  
      
  
      // 添加主函数
      code += `FUNCTION ${formData.interfaceFunction}()\n`;    
      code += `    LET g_forupd_sql = "${formData.sql}"\n\n`;
      code += `    RETURN g_forupd_sql\n`;
      code += `END FUNCTION\n`;
  
      return code;
    };
  
    return {
      generateAutoGpApiCode
    };
  };